package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.data.adapter.FilesAdapter
import com.spark.data.model.FileListModel
import kotlinx.android.synthetic.main.frag_select_file.*

/**
 * select the file to transfer
 */
class FilesFragment : BaseFragment() {
    private lateinit var filesAdapter: FilesAdapter
    private var file: FileListModel? = null

    companion object {
        val res_files = 0x223
        fun newInstance(): FilesFragment {
            val fragment = FilesFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_select_file
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        /**
         * affairm
         */
        tv_sure.setOnClickListener {
            val results = filesAdapter.results
            val bundle = Bundle()
            bundle.putString("files", Gson().toJson(results))
            setFragmentResult(res_files, bundle)
            pop()
        }
        iv_filter.setOnClickListener {
            val filesFilterFragment = FilesFilterFragment.newInstance()
            filesFilterFragment.show(childFragmentManager, "filefilter")
            filesFilterFragment.addSearchActionListener {
                fetchFiles(it.keyword, it.directory)
            }
        }
    }

    private fun initAdpater() {
        filesAdapter = FilesAdapter(file)
        exlv_files.setAdapter(filesAdapter)
        exlv_files.setGroupIndicator(null)
        exlv_files.divider = null
        exlv_files.setOnGroupClickListener { _, _, _, _ -> return@setOnGroupClickListener false }
        exlv_files.setOnChildClickListener { _, _, _, _, _ -> return@setOnChildClickListener false }

        file?.resultMaps?.forEachIndexed { index, resultMap -> exlv_files.expandGroup(index) }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        //fetchFiles()
        file = FileListModel(resultMaps = mutableListOf(
                FileListModel.ResultMap(name = "系统目录",fileList = mutableListOf(
                        FileListModel.ResultMap.File(filename = "子系统1"),
                        FileListModel.ResultMap.File(filename = "子系统2"),
                        FileListModel.ResultMap.File(filename = "子系统3")
                )), FileListModel.ResultMap(name = "系统目录",fileList = mutableListOf(
                        FileListModel.ResultMap.File(filename = "子系统1"),
                        FileListModel.ResultMap.File(filename = "子系统2"),
                        FileListModel.ResultMap.File(filename = "子系统3")
                )), FileListModel.ResultMap(name = "系统目录",fileList = mutableListOf(
                        FileListModel.ResultMap.File(filename = "子系统1"),
                        FileListModel.ResultMap.File(filename = "子系统2"),
                        FileListModel.ResultMap.File(filename = "子系统3")
                )), FileListModel.ResultMap(name = "系统目录",fileList = mutableListOf(
                        FileListModel.ResultMap.File(filename = "子系统1"),
                        FileListModel.ResultMap.File(filename = "子系统2"),
                        FileListModel.ResultMap.File(filename = "子系统3")
                )), FileListModel.ResultMap(name = "系统目录",fileList = mutableListOf(
                        FileListModel.ResultMap.File(filename = "子系统1"),
                        FileListModel.ResultMap.File(filename = "子系统2"),
                        FileListModel.ResultMap.File(filename = "子系统3")
                )), FileListModel.ResultMap(name = "系统目录", fileList = mutableListOf(
                FileListModel.ResultMap.File(filename = "子系统1"),
                FileListModel.ResultMap.File(filename = "子系统2"),
                FileListModel.ResultMap.File(filename = "子系统3")
        ))

        ))
        initAdpater()
    }

    @SuppressLint("CheckResult")
    private fun fetchFiles(keyword: String = "", privateFolderid: String = "") {
        val params = mutableMapOf(
                Pair("keyWords", keyword),
                Pair("privatefolderId", privateFolderid)
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_trans_file_list}", if (keyword.isNullOrBlank() && privateFolderid.isNullOrBlank()) "{}".toJsonBody() else params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, FileListModel::class.java)?.apply {
                                this@FilesFragment.file = this
                                initAdpater()
                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                        },
                        {},
                        {
                            dispose = it
                        }
                )
    }
}

